package com.dms.modules.product.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.dms.common.api.ApiResponse;
import com.dms.modules.product.entity.ProductFavorite;
import com.dms.modules.product.service.ProductFavoriteService;
import com.dms.modules.product.vo.ProductVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@Api(tags = "商品收藏管理")
@RestController
@RequestMapping("/api/v1/products/favorites")
public class ProductFavoriteController extends BaseProductController {

    @Autowired
    private ProductFavoriteService productFavoriteService;

    @PostMapping("/{productId}")
    @ApiOperation("添加收藏")
    public ApiResponse<Boolean> addFavorite(@ApiParam("商品ID") @PathVariable Long productId) {
        Long userId = getUserId();
        return ApiResponse.success(productFavoriteService.addFavorite(userId, productId));
    }

    @DeleteMapping("/{productId}")
    @ApiOperation("取消收藏")
    public ApiResponse<Boolean> cancelFavorite(@ApiParam("商品ID") @PathVariable Long productId) {
        Long userId = getUserId();
        return ApiResponse.success(productFavoriteService.cancelFavorite(userId, productId));
    }

    @GetMapping("/users")
    @ApiOperation("获取收藏列表")
    public ApiResponse<IPage<ProductVO>> getUserFavorites(
            @ApiParam("页码") @RequestParam(defaultValue = "1") Integer pageNum,
            @ApiParam("每页数量") @RequestParam(defaultValue = "10") Integer pageSize) {
        Long userId = getUserId();
        return ApiResponse.success(productFavoriteService.getUserFavoriteProducts(userId, pageNum, pageSize));
    }

    @GetMapping("/products/{productId}/check")
    @ApiOperation("检查是否已收藏")
    public ApiResponse<Boolean> checkFavorite(@ApiParam("商品ID") @PathVariable Long productId) {
        Long userId = getUserId();
        return ApiResponse.success(productFavoriteService.isFavorite(userId, productId));
    }

    @GetMapping("/products/{productId}/count")
    @ApiOperation("获取商品收藏数量")
    public ApiResponse<Integer> getFavoriteCount(@ApiParam("商品ID") @PathVariable Long productId) {
        return ApiResponse.success(productFavoriteService.getFavoriteCount(productId));
    }
} 